#include "mainwidget.h"
#include "mybtn/mybutton.h"
#include <QPainter>
#include "global/globalvar.h"
#include "mybtn/imagebutton.h"
#include "commondef.h"
#include "global/myresource.h"
#include <QApplication>
#include "otherwidget/promptwidget.h"
#include "mybtn/difbtn.h"
#include <QParallelAnimationGroup>
#include <QPropertyAnimation>
#include "mybtn/backbtn.h"
#include <QDebug>
#include <QMouseEvent>
#include <QTranslator>

#define BTN_X                           60.0f
#define BTN_CONTINUE_Y                  180.0f
#define BTN_FIRST_Y                     260.0f
#define BTN_FIRST_DIF_Y                 260.0f
#define BTN_INTERVAL                    60.0f
#define BTN_W                           250.0f
#define BTN_DIF_W                       250.0f
#define BTN_H                           80.0f
#define BTN_DIF_H                       60.0f

#define DIF_BTN_ORI_X                   365

#define FRESH_BTN_ORI_RC                QRect(DIF_BTN_ORI_X,BTN_FIRST_DIF_Y,BTN_DIF_W,BTN_DIF_H)
#define NORMAL_BTN_ORI_RC               QRect(DIF_BTN_ORI_X,BTN_FIRST_DIF_Y + BTN_INTERVAL,BTN_DIF_W,BTN_DIF_H)
#define MASTER_BTN_ORI_RC               QRect(DIF_BTN_ORI_X,BTN_FIRST_DIF_Y + BTN_INTERVAL * 2,BTN_DIF_W,BTN_DIF_H)
#define GUN_BTN_ORI_RC                  QRect(DIF_BTN_ORI_X,BTN_FIRST_DIF_Y + BTN_INTERVAL * 3,BTN_DIF_W,BTN_DIF_H)
#define BACK_BTN_ORI_RC                 QRect(0,WINDOW_HEIGHT - 60,80,60)

#define FRESH_BTN_TERM_RC               QRect(BTN_X,BTN_FIRST_DIF_Y,BTN_DIF_W,BTN_DIF_H)
#define NORMAL_BTN_TERM_RC              QRect(BTN_X,BTN_FIRST_DIF_Y + BTN_INTERVAL,BTN_DIF_W,BTN_DIF_H)
#define MASTER_BTN_TERM_RC              QRect(BTN_X,BTN_FIRST_DIF_Y + BTN_INTERVAL * 2,BTN_DIF_W,BTN_DIF_H)
#define GUN_BTN_TERM_RC                 QRect(BTN_X,BTN_FIRST_DIF_Y + BTN_INTERVAL * 3,BTN_DIF_W,BTN_DIF_H)
#define BACK_BTN_TERM_RC                QRect(0,WINDOW_HEIGHT - 40,80,40)

MainWidget::MainWidget(QWidget *parent) :
    QWidget             (parent),
    m_promptWidget      (NULL)
{
    m_continueBtn = new MyButton(this,QSizeF(BTN_W,BTN_H));
    m_continueBtn->setGeometry(BTN_X,BTN_CONTINUE_Y,BTN_W,BTN_H);
    m_continueBtn->setText(tr("continue"),10);
    connect(m_continueBtn,SIGNAL(myclick()),this,SLOT(conti()));

    m_playBtn = new MyButton(this,QSizeF(BTN_W,BTN_H));
    m_playBtn->setGeometry(BTN_X,BTN_FIRST_Y,BTN_W,BTN_H);
    m_playBtn->setText(tr("play"),10);
    connect(m_playBtn,SIGNAL(myclick()),this,SLOT(play()));

    m_statisticsBtn = new ImageButton(this,
                                      QSizeF(BTN_INFO_WIDTH,BTN_INFO_HEIGHT),
                                      *MyResource::m_staticbtnpImage,
                                      *MyResource::m_staticbtnImage);
    m_statisticsBtn->setGeometry(0,
                                 WINDOW_HEIGHT - BTN_INFO_HEIGHT * 2 - 20,
                                 BTN_INFO_WIDTH,BTN_INFO_HEIGHT);
    connect(m_statisticsBtn,SIGNAL(myclick()),this,SLOT(statistics()));

    m_quitBtn = new ImageButton(this,
                                QSizeF(BTN_INFO_WIDTH,BTN_INFO_HEIGHT),
                                *MyResource::m_quitbtnpImage,
                                *MyResource::m_quitbtnImage);
    m_quitBtn->setGeometry(WINDOW_WIDTH - BTN_INFO_WIDTH,
                           WINDOW_HEIGHT - BTN_INFO_HEIGHT,BTN_INFO_WIDTH,BTN_INFO_HEIGHT);
    connect(m_quitBtn,SIGNAL(myclick()),this,SLOT(quit()));

    m_setBtn = new ImageButton(this,
                               QSizeF(BTN_INFO_WIDTH,BTN_INFO_HEIGHT),
                               *MyResource::m_setbtnpImage,
                               *MyResource::m_setbtnImage);
    m_setBtn->setGeometry(0.0f,WINDOW_HEIGHT - BTN_INFO_HEIGHT,BTN_INFO_WIDTH,BTN_INFO_HEIGHT);
    connect(m_setBtn,SIGNAL(myclick()),this,SLOT(MainToSet()));

    m_infoBtn = new ImageButton(this,
                                QSizeF(BTN_INFO_WIDTH,BTN_INFO_HEIGHT),
                                *MyResource::m_infobtnpImage,
                                *MyResource::m_infobtnImage);
    m_infoBtn->setGeometry(WINDOW_WIDTH - BTN_INFO_WIDTH,
                           WINDOW_HEIGHT - BTN_INFO_HEIGHT * 2 - 20,
                           BTN_INFO_WIDTH,BTN_INFO_HEIGHT);
    connect(m_infoBtn,SIGNAL(myclick()),this,SLOT(AboutToMain()));

    m_freshmanBtn = new DifBtn(this,globalvar::m_strfresh,false);
    m_freshmanBtn->setGeometry(FRESH_BTN_ORI_RC);
    connect(m_freshmanBtn,SIGNAL(myclick()),this,SLOT(freshman()));

    m_normalBtn = new DifBtn(this,globalvar::m_strnormal);
    m_normalBtn->setGeometry(NORMAL_BTN_ORI_RC);
    int ilevel = globalvar::level();
    if(ilevel >= NORMAL_L)
    {
        m_normalBtn->m_block = false;
    }
    connect(m_normalBtn,SIGNAL(myclick()),this,SLOT(general()));

    m_masterBtn = new DifBtn(this,globalvar::m_strmaster);
    m_masterBtn->setGeometry(MASTER_BTN_ORI_RC);
    if(ilevel >= MASTER_L)
    {
        m_masterBtn->m_block = false;
    }
    connect(m_masterBtn,SIGNAL(myclick()),this,SLOT(master()));

    m_biggunBtn = new DifBtn(this,globalvar::m_strgun);
    m_biggunBtn->setGeometry(GUN_BTN_ORI_RC);
    if(ilevel >= GUN_L)
    {
        m_biggunBtn->m_block = false;
    }
    connect(m_biggunBtn,SIGNAL(myclick()),this,SLOT(biggun()));

    m_backBtn = new BackBtn(this);
    m_backBtn->setGeometry(BACK_BTN_ORI_RC);
    connect(m_backBtn,SIGNAL(myclick()),this,SLOT(DiftoMain()));
    m_backBtn->setVisible(false);
}

MainWidget::~MainWidget()
{
}

void MainWidget::conti()
{
    globalvar::m_logic.ContinueInit();

    emit continuegame();
}

void MainWidget::play()
{
    ShowImageBtn(false);
    ShowDifBtnAni();
}

void MainWidget::statistics()
{
    emit maintoStatis();
}

void MainWidget::quit()
{
    if(m_promptWidget == NULL)
    {
        m_promptWidget = new PromptWidget(this,tr("sure you exit the game?"));
        m_promptWidget->setGeometry(0,0,WINDOW_WIDTH,WINDOW_HEIGHT);
        m_promptWidget->showFullScreen();
        connect(m_promptWidget,SIGNAL(click(int)),this,SLOT(promptback(int)));
    }
}

void MainWidget::paintEvent(QPaintEvent *)
{
    QPainter p(this);

    QFont f("Times [Cronyx]",15);//"Times [Cronyx]", "Palatino". "Helvetica [Cronyx]".
    p.setFont(f);

    QString m_strGreet = globalvar::name();
    QString m_strw = tr(",welcome :)");
    if(m_strGreet.length() > 5)
    {
        p.drawText(QRectF(0.0f,60.0f,360.0f,80.0f),Qt::AlignCenter,m_strGreet);
        p.drawText(QRectF(0.0f,120.0f,360.0f,80.0f),
                   Qt::AlignCenter,
                   m_strw);
    }
    else
    {
        p.drawText(QRectF(0.0f,60.0f,360.0f,80.0f),Qt::AlignCenter,m_strGreet + m_strw);
    }
}

void MainWidget::promptback(int i)
{
    if(m_promptWidget != NULL)
    {
        delete m_promptWidget;
        m_promptWidget = NULL;
    }

    switch(i)
    {
    case YES_PTP:
    {
        QApplication::instance()->exit();
    }
        break;
    case NC_PTP:
    {
        globalvar::iCurlevel = NEW_L;

        emit playgame();
    }
        break;
    case NOC_PTP:
    {
        globalvar::iCurlevel = NORMAL_L;

        emit playgame();
    }
        break;
    case MC_PTP:
    {
        globalvar::iCurlevel = MASTER_L;

        emit playgame();
    }
        break;
    case GC_PTP:
    {
        globalvar::iCurlevel = GUN_L;

        emit playgame();
    }
        break;
    default:
        break;
    }
}

void MainWidget::freshman()
{
//    QCoreApplication::instance()->removeTranslator(globalvar::m_trans);
//    delete globalvar::m_trans;
//    globalvar::m_trans = new QTranslator();
//    globalvar::m_trans->load(QString("sudoku_zh_TW"));
//    QCoreApplication::instance()->installTranslator(globalvar::m_trans);

    if(globalvar::IsShowContinue())
    {
        if(m_promptWidget == NULL)
        {
            m_promptWidget = new PromptWidget(this,
                                              tr("Are you sure you want to give up already saved games?"),
                                              NC_PTP);
            m_promptWidget->setGeometry(0,0,WINDOW_WIDTH,WINDOW_HEIGHT);
            m_promptWidget->showFullScreen();
            connect(m_promptWidget,SIGNAL(click(int)),this,SLOT(promptback(int)));
        }
    }
    else
    {

        globalvar::iCurlevel = NEW_L;

        emit playgame();
    }
}

void MainWidget::general()
{
    if(m_normalBtn->m_block)
        return;

    if(globalvar::IsShowContinue())
    {
        if(m_promptWidget == NULL)
        {
            m_promptWidget = new PromptWidget(this,
                                              tr("Are you sure you want to give up already saved games?"),
                                              NOC_PTP);
            m_promptWidget->setGeometry(0,0,WINDOW_WIDTH,WINDOW_HEIGHT);
            m_promptWidget->showFullScreen();
            connect(m_promptWidget,SIGNAL(click(int)),this,SLOT(promptback(int)));
        }
    }
    else
    {

        globalvar::iCurlevel = NORMAL_L;

        emit playgame();
    }
}

void MainWidget::master()
{
    if(m_masterBtn->m_block)
        return;

    if(globalvar::IsShowContinue())
    {
        if(m_promptWidget == NULL)
        {
            m_promptWidget = new PromptWidget(this,
                                              tr("Are you sure you want to give up already saved games?"),
                                              MC_PTP);
            m_promptWidget->setGeometry(0,0,WINDOW_WIDTH,WINDOW_HEIGHT);
            m_promptWidget->showFullScreen();
            connect(m_promptWidget,SIGNAL(click(int)),this,SLOT(promptback(int)));
        }
    }
    else
    {

        globalvar::iCurlevel = MASTER_L;

        emit playgame();
    }
}

void MainWidget::biggun()
{
    if(m_biggunBtn->m_block)
        return;

    if(globalvar::IsShowContinue())
    {
        if(m_promptWidget == NULL)
        {
            m_promptWidget = new PromptWidget(this,
                                              tr("Are you sure you want to give up already saved games?"),
                                              GC_PTP);
            m_promptWidget->setGeometry(0,0,WINDOW_WIDTH,WINDOW_HEIGHT);
            m_promptWidget->showFullScreen();
            connect(m_promptWidget,SIGNAL(click(int)),this,SLOT(promptback(int)));
        }
    }
    else
    {

        globalvar::iCurlevel = GUN_L;

        emit playgame();
    }
}

void MainWidget::ShowImageBtn(bool bshow)
{
    m_statisticsBtn->setVisible(bshow);
    m_quitBtn->setVisible(bshow);
    m_setBtn->setVisible(bshow);
    m_infoBtn->setVisible(bshow);
}

void MainWidget::ShowDifBtnAni()
{
    QPropertyAnimation *freshani = new QPropertyAnimation(m_freshmanBtn, "pos");
    freshani->setDuration(1000);
    freshani->setKeyValueAt(0,QPoint(DIF_BTN_ORI_X,BTN_FIRST_DIF_Y));
    freshani->setKeyValueAt(0.6,QPoint(BTN_X,BTN_FIRST_DIF_Y));
    freshani->setKeyValueAt(0.8,QPoint(BTN_X + 50,BTN_FIRST_DIF_Y));
    freshani->setKeyValueAt(1,QPoint(BTN_X,BTN_FIRST_DIF_Y));

    QPropertyAnimation *normalani = new QPropertyAnimation(m_normalBtn, "pos");
    normalani->setDuration(1200);
    normalani->setKeyValueAt(0,QPoint(DIF_BTN_ORI_X,BTN_FIRST_DIF_Y + BTN_INTERVAL));
    normalani->setKeyValueAt(0.6,QPoint(BTN_X,BTN_FIRST_DIF_Y + BTN_INTERVAL));
    normalani->setKeyValueAt(0.8,QPoint(BTN_X + 50,BTN_FIRST_DIF_Y + BTN_INTERVAL));
    normalani->setKeyValueAt(1,QPoint(BTN_X,BTN_FIRST_DIF_Y + BTN_INTERVAL));

    QPropertyAnimation *masterani = new QPropertyAnimation(m_masterBtn, "pos");
    masterani->setDuration(1400);
    masterani->setKeyValueAt(0,QPoint(DIF_BTN_ORI_X,BTN_FIRST_DIF_Y + BTN_INTERVAL * 2));
    masterani->setKeyValueAt(0.6,QPoint(BTN_X,BTN_FIRST_DIF_Y + BTN_INTERVAL * 2));
    masterani->setKeyValueAt(0.8,QPoint(BTN_X + 50,BTN_FIRST_DIF_Y + BTN_INTERVAL * 2));
    masterani->setKeyValueAt(1,QPoint(BTN_X,BTN_FIRST_DIF_Y + BTN_INTERVAL * 2));

    QPropertyAnimation *gunani = new QPropertyAnimation(m_biggunBtn, "pos");
    gunani->setDuration(1600);
    gunani->setKeyValueAt(0,QPoint(DIF_BTN_ORI_X,BTN_FIRST_DIF_Y + BTN_INTERVAL * 3));
    gunani->setKeyValueAt(0.6,QPoint(BTN_X,BTN_FIRST_DIF_Y + BTN_INTERVAL * 3));
    gunani->setKeyValueAt(0.8,QPoint(BTN_X + 50,BTN_FIRST_DIF_Y + BTN_INTERVAL * 3));
    gunani->setKeyValueAt(1,QPoint(BTN_X,BTN_FIRST_DIF_Y + BTN_INTERVAL * 3));

    QPropertyAnimation *playani = new QPropertyAnimation(m_playBtn,"pos");
    playani->setDuration(100);
    playani->setKeyValueAt(0,QPoint(BTN_X,BTN_FIRST_Y));
    playani->setKeyValueAt(1,QPoint(-BTN_W - 5,BTN_FIRST_Y));

    QParallelAnimationGroup  *group = new QParallelAnimationGroup;
    connect(group,SIGNAL(finished()),this,SLOT(mainanifinishSB()));
    group->addAnimation(freshani);
    group->addAnimation(playani);
    group->addAnimation(normalani);
    group->addAnimation(gunani);
    group->addAnimation(masterani);
    group->start();
}

void MainWidget::DiftoMain()
{
    ShowMainBtnAni();
}

void MainWidget::ShowMainBtnAni()
{
    QPropertyAnimation *freshani = new QPropertyAnimation(m_freshmanBtn, "geometry");
    freshani->setDuration(100);
    freshani->setKeyValueAt(0,QRect(FRESH_BTN_TERM_RC));
    freshani->setKeyValueAt(1,QRect(FRESH_BTN_ORI_RC));

    QPropertyAnimation *normalani = new QPropertyAnimation(m_normalBtn, "geometry");
    normalani->setDuration(100);
    normalani->setKeyValueAt(0,QRect(NORMAL_BTN_TERM_RC));
    normalani->setKeyValueAt(1,QRect(NORMAL_BTN_ORI_RC));

    QPropertyAnimation *masterani = new QPropertyAnimation(m_masterBtn, "geometry");
    masterani->setDuration(100);
    masterani->setKeyValueAt(0,QRect(MASTER_BTN_TERM_RC));
    masterani->setKeyValueAt(1,QRect(MASTER_BTN_ORI_RC));

    QPropertyAnimation *gunani = new QPropertyAnimation(m_biggunBtn, "geometry");
    gunani->setDuration(100);
    gunani->setKeyValueAt(0,QRect(GUN_BTN_TERM_RC));
    gunani->setKeyValueAt(1,QRect(GUN_BTN_ORI_RC));

    QPropertyAnimation *playani = new QPropertyAnimation(m_playBtn,"pos");
    playani->setDuration(1200);
    playani->setKeyValueAt(0,QPoint(-BTN_W - 5,BTN_FIRST_Y));
    playani->setKeyValueAt(0.6,QPoint(BTN_X,BTN_FIRST_Y));
    playani->setKeyValueAt(0.8,QPoint(BTN_X - 30,BTN_FIRST_Y));
    playani->setKeyValueAt(1,QPoint(BTN_X,BTN_FIRST_Y));

    QParallelAnimationGroup  *group = new QParallelAnimationGroup;
    group->addAnimation(freshani);
    group->addAnimation(playani);
    group->addAnimation(normalani);
    group->addAnimation(gunani);
    group->addAnimation(masterani);
    group->start();
    connect(group,SIGNAL(finished()),this,SLOT(mainanifinish()));
    m_backBtn->setVisible(false);
}

void MainWidget::mainanifinish()
{
    ShowImageBtn(true);
}

void MainWidget::mainanifinishSB()
{
    m_backBtn->setVisible(true);
}

void MainWidget::AboutToMain()
{
    emit AboutToM();
}

void MainWidget::MainToSet()
{
    emit mtos();
}

void MainWidget::Unlock(int l)
{
    switch(l)
    {
    case NORMAL_L:
    {
        m_normalBtn->m_block = false;
    }
        break;
    case MASTER_L:
    {
        m_masterBtn->m_block = false;
    }
        break;
    case GUN_L:
    {
        m_biggunBtn->m_block = false;
    }
        break;
    default:
        break;
    }
}

void MainWidget::ShowContinueBtn()
{
    m_continueBtn->setVisible(globalvar::IsShowContinue());
}

void MainWidget::mousePressEvent(QMouseEvent *e)
{
    if(QRectF(0,WINDOW_HEIGHT - 60,80,60).contains(e->posF()))
    {
        ShowMainBtnAni();
    }
}

